package com.zhaolang.qogir.domain.impl;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.zhaolang.qogir.cache.SharePreCacheHelper;
import com.zhaolang.qogir.domain.BaseCall;
import com.zhaolang.qogir.domain.BaseResp;
import com.zhaolang.qogir.domain.PersonalInfoManage;
import com.zhaolang.qogir.server.ClientCallbackImpl;
import com.zhaolang.qogir.server.RadarProxy;
import com.zhaolang.qogir.server.ServerRequestParams;
import com.zhaolang.qogir.util.HttpConstant;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class PersonalInfoManageImpl extends PersonalInfoManage {
    private Handler handler1;
    private Handler handler2;
    private Handler handler3;
    private Context context;
    private ServerRequestParams params;

    public PersonalInfoManageImpl(Context context) {
        this.context = context;
    }

    // 修改性别
    @Override
    public void retrieveGenderAsync(String bean, final BaseCall<BaseResp> call) {
        handler3 = new Handler() {
            public void handleMessage(Message msg) {
                if (call != null && !call.cancel) {
                    call.call((BaseResp) msg.obj);
                }
            }
        };
        RadarProxy.getInstance(context).startServerData(writeGenderParams(bean),
                new ClientCallbackImpl() {
                    @Override
                    public void onSuccess(String result) {
                        JSONObject jsonObject;
                        BaseResp resp = new BaseResp();
                        Log.d("Radar", "retrieveGenderAsync: " + result);
                        try {
                            jsonObject = new JSONObject(result);
                            JSONObject jsonObject2 = new JSONObject(jsonObject.optString("message"));
                            resp.setSuccess(jsonObject2.optBoolean("success"));//true,false
                            resp.setStatusCode(jsonObject2.optInt("statusCode"));

                            JSONObject jsonObject3 = new JSONObject(jsonObject2.optString("message"));
                            resp.setMessage(jsonObject3.optString("msg"));// ok
                            resp.setStatus(jsonObject3.optString("status"));//SUCCESS
                        } catch (JSONException e) {
                            e.printStackTrace();
                            resp.setStatus("FAILED");
                            Log.d("Radar", "JSONException: " + e);
                        }
                        Message msg = Message.obtain();
                        msg.obj = resp;
                        handler3.sendMessage(msg);
                    }

                    @Override
                    public void onFailure(String result) {
                        BaseResp resp = new BaseResp();
                        resp.setStatus("FAILED");
                        Message msg = Message.obtain();
                        msg.obj = resp;
                        handler3.sendMessage(msg);
                        System.out.println(result);
                    }
                });
    }

    private ServerRequestParams writeGenderParams(String gender) {
        params = getServerRequest();
//        params.setRequestUrl(HttpConstant.updateGender(null));
        Map<String, Object> param = new HashMap<String, Object>();
        String username = SharePreCacheHelper.getUserName(context);
        String userId = SharePreCacheHelper.getUserID(context);
        param.put("USER_ID", userId);
        param.put("GENDER", gender);
        param.put("TOKEN", HttpConstant.TOKEN);
        param.put("FKEY", HttpConstant.getFKEY());
        params.setRequestParam(param);
        return params;
    }

    private ServerRequestParams getServerRequest() {
        if (params == null) {
            params = new ServerRequestParams();
        }
        return params;
    }
}
